home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / vol.c < prev    next >
C/C++ Source or Header  |  1990-07-15  |  3KB  |  123 lines

  1. /* vol - break stdin into volumes    Author: Andy Tanenbaum */
  2.  
  3. /* This program reads standard input and writes it onto diskettes, pausing
  4.  * at the start of each one.  It's main use is for saving files that are
  5.  * larger than a single diskette.  Vol just writes its standard input onto
  6.  * a diskette, and prompts for a new one when it is full.  This mechanism
  7.  * is transparent to the process producing vol's standard input. For example,
  8.  *    tar c - . | vol 360 /dev/fd0
  9.  * puts the tar output as as many diskettes as needed.  To read them back in,
  10.  * use
  11.  *    vol -u 360 /dev/fd0 | tar x -
  12.  *
  13.  */
  14.  
  15. #include <sys/types.h>
  16. #include <fcntl.h>
  17. #include <sys/stat.h>
  18. #include <blocksize.h>
  19. #include <signal.h>
  20. #include <errno.h>
  21.  
  22. extern int errno;
  23. extern char *itoa();
  24.  
  25. char buffer[BLOCK_SIZE];
  26.  
  27. main(argc, argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.   int volume = 1, size, reading, fd, tty;
  32.   char *p, *name;
  33.   struct stat stb;
  34.  
  35.   signal(SIGPIPE, SIG_IGN);
  36.  
  37.   /* Fetch and verify the arguments. */
  38.   if (argc != 3 && argc != 4)
  39.     message("Usage: vol [-u] size block-special\n", "");
  40.   p = argv[1];
  41.   reading = (*p == '-' && *(p + 1) == 'u' ? 1 : 0);
  42.   size = atoi(argv[reading + 1]);
  43.   name = argv[reading + 2];
  44.   tty = open("/dev/tty", O_RDONLY);
  45.  
  46.   if (size <= 0) message("vol: bad volume size\n", argv[reading + 1]);
  47.   if (stat(name, &stb) < 0) message("vol: cannot stat %s\n", name);
  48.   if ((stb.st_mode & S_IFMT) != S_IFBLK)
  49.     message("vol: %s is not a block special file\n", name);
  50.   if (tty < 0) message("vol: cannot open /dev/tty\n", "");
  51.  
  52.   while (1) {
  53.     /* Open the special file. */
  54.     fd = open(name, 1 - reading);
  55.     if (fd < 0) message("vol: cannot open %s\n", name);
  56.  
  57.     std_err("◆Please insert volume ");
  58.     num(volume);
  59.     std_err(" and hit return\n");
  60.     read(tty, buffer, BLOCK_SIZE);
  61.     volume++;
  62.  
  63.     /* Read or write the requisite number of blocks. */
  64.     if (reading)
  65.         diskio(size, fd, 1, name, "stdout");    /* vol -u | tar xf - */
  66.     else
  67.         diskio(size, 0, fd, "stdin", name);    /* tar cf - | vol */
  68.  
  69.     close(fd);
  70.   }
  71. }
  72.  
  73. diskio(size, fd1, fd2, errstr1, errstr2)
  74. int size, fd1, fd2;
  75. char *errstr1, *errstr2;
  76. {
  77. /* Read 'size' blocks from 'fd1' and write them on 'fd2'.  Watch out for
  78.  * the fact that reads on pipes can return less than the desired data.
  79.  */
  80.  
  81.   int n, m, count;
  82.   long needed;
  83.  
  84.   needed = (long) BLOCK_SIZE *(long) size;    /* # bytes to read */
  85.   while (needed > 0L) {
  86.     count = (needed > (long) BLOCK_SIZE ? BLOCK_SIZE : (int) needed);
  87.     n = read(fd1, buffer, count);
  88.     if (n == 0) exit(0);
  89.     if (n < 0) message("Error encountered while reading %s\n", errstr1);
  90.     m = write(fd2, buffer, n);
  91.     if (m < 0 && errno == SIGPIPE) exit(0);
  92.     if (m > 0 && m != n) message("Output error on %s\n", errstr2);
  93.     if (m < 0) message("Error encountered while writing %s\n", errstr2);
  94.     needed -= n;
  95.   }
  96. }
  97.  
  98.  
  99.  
  100. message(s1, s2)
  101. char *s1, *s2;
  102. {
  103.   printf(s1, s2);
  104.   exit(1);
  105. }
  106.  
  107. num(n)
  108. int n;
  109. {
  110.   char out[3];
  111.  
  112.   out[0] = ' ';
  113.   out[1] = '0';
  114.   out[2] = 0;
  115.   if (n < 10) {
  116.     out[1] += n;
  117.   } else {
  118.     out[1] += (n % 10);
  119.     out[0] = '0' + (n / 10);
  120.   }
  121.   std_err(out);
  122. }
  123.